package redsig.redaction;

import java.util.ArrayList;

import javax.swing.tree.DefaultMutableTreeNode;

import core.Adapter;


import data.XMLNode;

/**
 * @author  Bacher
 */
public class Redacter {
	
	ArrayList<DefaultMutableTreeNode> selectedNodes = new ArrayList<DefaultMutableTreeNode>();
	ArrayList<XMLNode> selectedXMLNodes = new ArrayList<XMLNode>();
	/**
	 * @uml.property  name="rootNode"
	 * @uml.associationEnd  
	 */
	private XMLNode rootNode;
	private static boolean redacted = false;
	private long now;
	
	public Redacter(){
		
	}
	
	public Redacter(ArrayList<DefaultMutableTreeNode> selectedNodes, DefaultMutableTreeNode rootNode){
		now = System.currentTimeMillis();
		this.selectedNodes = selectedNodes;
		
		for(DefaultMutableTreeNode node : this.selectedNodes){
			if(Adapter.deepestLevel < node.getLevel())
				Adapter.deepestLevel = node.getLevel();
			selectedXMLNodes.add(Adapter.getMap().get(node));
		}
		
		this.rootNode = Adapter.getMap().get(rootNode);
	}
	
	public void redact(){
		for(XMLNode node : selectedXMLNodes){
			node.setRedacted(true);	
			Adapter.numRedacts++;
		}
		//changed on 14.11.2011
		//publishSalts(rootNode);
		for(XMLNode node : selectedXMLNodes){
			publishSalts(node);
		}
		
		Adapter.millisForRedaction = System.currentTimeMillis() - now;
	
	}
	
	
//	public void publishSalts(XMLNode node){
//		if(node.getChildren().isEmpty()){
//			node.setPublishSalt(true);
//			System.out.println(node.getName());
//		}
//		else{
//			for(XMLNode child : node.getChildren())
//			{
//				publishSalts(child);
//			}
//		}
//	}
	
	//changed on 14.11.2011
	public void publishSalts(XMLNode node){
		//trivial case: root node
		if(node.getParent() == null){
			node.setPublishSalt(false);
		}
		//publish salts of all siblings along the path to the root
		else{
			ArrayList<XMLNode> siblings = node.getSiblings();
			for (XMLNode xmlNode : siblings) {
				if(!xmlNode.isRedacted()){
					xmlNode.setPublishSalt(true);
					Adapter.numSalts++;
				}else xmlNode.setPublishSalt(false);
			}
			publishSalts(node.getParent());
		}
	}
	
	
/*	
	public static boolean checkForRedaction(XMLNode document){
		if(document.isRedacted()){
			return true;
		}
		else{
			for(XMLNode child : document.getChildren()){
				return checkForRedaction(child, false);
			}
		}
	}
*/	
	public static boolean checkForRedaction(XMLNode document, boolean redacted){
		if(document.isRedacted()){
			redacted = true;
		}
		else if(redacted == false){
			for(XMLNode child : document.getChildren()){
				redacted = checkForRedaction(child, redacted);
			}
		}
		return redacted;
	}
	
	/*
	private void publishSalts(XMLNode node){
		
		if(node.getParent() == null){
			return;
		}
		else{
			ArrayList<XMLNode> siblings = node.getSiblings();
			for(XMLNode sibling : siblings){
				if(check(sibling) == true){
					sibling.setPublishSalt(true);
				}
					
			}
			publishSalts(node.getParent());
		}
	}
	*/
}
